<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML>
<html style='height:100%;'>
<head>
<base href="<%=basePath%>">

<title>巡查新增</title>
<link rel="stylesheet" href="css/vivo.css">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="js/bootstrap2/css/bootstrap.min.css">
<link rel="stylesheet" href="css/common.css">
<link href="js/select2/select2.min.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" type="text/css"
	href="static/p_house/house_index.css" />
<link rel="stylesheet" type="text/css"
	href="js/webuploader-0.1.5/webuploader.css" />

<script src="js/jquery-1.11.1.js"></script>
<script src="js/bootstrap2/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/layui-v2.1.5/layui/layui.all.js"></script>
<script type="text/javascript" src="<%=basePath%>js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="js/webuploader-0.1.5/webuploader.js"></script>
<script type="text/javascript" src="js/sharefunction.js"></script>
<script src="js/select2/select2.min.js"></script>
<script type="text/javascript" src="js/form.js"></script>
<!-- 图片放大 -->
<script src="js/postbird-img-glass/postbird-img-glass.js"></script>
<!-- 装修巡查 -->
<script type="text/javascript">
	var s2ExamineContent;
	$(document).ready(function() {
		s2ExamineContent = $("#examineContent").select2({
			tags : true
		});
		initExamineContent();
		s2ExamineContent.val("人员巡查").trigger("change");
		//初始赋值
		initManMsg();
		if(manFlag == "2"){
			$("#dm_div").show();
		}
		initWebUpload("filePicker_wgxx", "list_wgxx", "违规现象", "decoration_articleId", decorationSuccess, decorationErr);
		//上传按钮添加默认宽高
		$("div[id^='filePicker_'] div").css({
				"width" : "60px",
				"height" : "28px"
			});
	});
	//展示附件
	function getFiles() {
		getFileList($("#man_div #entityId_id").val(), "违规现象", "#list_wgxx", setRFL, imgPlus);
		
	}
	function imgPlus(data) {
		$("#man_div .uploadcancel").css("display", "none");
		$.each(data, function(i, v) {
			//图片放大
			PostbirdImgGlass.init({
				domSelector : "#" + v.id + " img",
				animation : true,
				myDocument : window.parent.parent.parent
			});
		});
	}
  //上传成功回调
	function decorationSuccess(file, response) {
		PostbirdImgGlass.init({
			domSelector : "td img",
			animation : true,
			myDocument : window.parent.parent.parent
		});
	}
	var rFileList = ""; //
	//设置待删除附件的值到集合中
	function setRFL(id, _this) {
		layer.confirm('删除该图片？', {
			title : '警告',
			icon : 0,
			shade : [ 0.1, '#fff' ]
		}, function() {
			//rFileList.push(id);
			rFileList=id+","+rFileList
			//删除指定缩略图
			$("#" + id).remove();
			layer.close(layer.index);
		});
	}
	//上传失败回调
	function decorationErr(file, data) {
		//alert("上传失败回调");
	}
	//初始化赋值
	var manFlag = '${manFlag}';//查看1、编辑2状态
	function initManMsg() {
		var id = $("#man_div").find("#entityId_id").val();
		if (id != null && id != "") {
			$.ajaxSettings.async = false;
			$.post("customer/dm/getManById", {
				id : id
			}, function(data, status) {
				if (data != null) {
					
					$("#man_div input[name='examineDate']").val(new Date(data.examineDate).format('yyyy-MM-dd hh:mm:ss'));
					$("#man_div [name='fkexaminePerson']").val(data.fkexaminePerson);
					$("#man_div textarea[name='examineRecord']").val(data.examineRecord);
					$("#man_div #isfraud[value= " + data.isfraud + "]").attr("checked", true);
					$("#man_div textarea[name='fraudDispose']").val(data.fraudDispose);
					$("#man_div input[name='fraudImg']").val(data.fraudImg);
					s2ExamineContent.val(data.examineContent).trigger("change");
					//违规状态为1时，展示违规内容
					if(data.isfraud==1){
						$(".weigui").show();
					}
					getFiles();
					if(manFlag == "1"){//查看
						//隐藏按钮
						$("#man_div #btn_div2").hide();
						$("#man_div #btn_div1").hide();
						$("#man_div").find("input,textarea").attr("readonly", true);
						$("#man_div").find(".addbtn,select,input[type='file'],.Wdate").attr("disabled", true);
						$("#man_div input[name='isfraud']").attr("disabled", true);
					}else{
						//上传按钮水平排列
						$("div[id^='filePicker_']").css({"display":"inline-block","vertical-align":"bottom"});
						$("#man_div #btn_div2").show();
						$("#man_div #btn_div1").hide();
						$("#man_div .uploadcancel").show();
						$("#man_div input[name='isfraud']").attr("disabled", false);
					}
				}
			});
		}else{
			//上传按钮水平排列
			$("div[id^='filePicker_']").css({"display":"inline-block","vertical-align":"bottom"});
			$("#man_div #btn_div2").hide();
			$("#man_div #btn_div1").show();
			$("#man_div input[name='isfraud']").attr("disabled", false);
		}
	}

	var optMan;
	//optHide操作字段，0：临时保存；1：新增；2：修改
	function saveMan(val) {
		optMan = val;
		if (val == 3) {
			//解除锁定状态
			var index = layer.confirm('确定开启编辑状态？', {
				offset : '20%',
				shade : 0
			}, function() {
				//去除只读开启按钮编辑
				$("#man_div").find("input,textarea").attr("readonly", false);
				$("#man_div").find("select,.addbtn,input[type='file'],.Wdate").attr("disabled", false);
				$("#man_div").find(".Wdate").attr("readonly", true);
				$("#man_div").find(".icon-fullscreen").show();
				$("#man_div #modifyBtn").hide();
				$("#man_div .hidbtn").addClass("showbtn");
				layer.close(index);
			});
		} else if(val == 2){
			var approvalCount = ${approvalCount};
			if(approvalCount == 0 ){
				//提交表单
				saveManage();
			}else{
				layer.msg("此条数据已提交过审批，不能重复提交");
			}
		}else{
			//$("#man_div").submit();
			saveManage();
		}
	}
	function initExamineContent() {
		var html = "";
		var arr = [ "人员巡查", "消防巡查", "安全巡查", "卫生巡查", "水电巡查", "超违处理" ];
		for (var i = 0; i < arr.length; i++) {
			html = "<option value=\"" + arr[i] + "\">" + arr[i] + "</option>";
			$("#examineContent").append(html);
		}
	}

	function change(obj) {
		var examine = $(obj).val();
		$("#examine").text(examine);
	}
	//隐藏违规
	function disWeigui(){
		var isfraud = $("input[name='isfraud']:checked").val();
		if(isfraud==0){
			$(".weigui").hide();
		}else{
			$(".weigui").show();
		}
	}

	function saveManage() {
		var i = layer.load(2);
		var date = new Date();
		var examineDate = $("#examineDate").val();
		var fraudImg = $("#fraudImg").val();
		var examineContent = $("#examineContent").val();
		var examineRecord = $("#examineRecord").val();
		var isfraud = $("input[name='isfraud']:checked").val();
		var AccessoryCaList = $("#decoration_articleId").val();
		var remark = $("#dmRemark").val();
		if(optMan == "2"){
			if(remark == "" || remark == undefined){
				layer.msg("变更说明不能为空");
				layer.close(i);
				return null;
			}
		}
		var exDate= new Date(examineDate);
		if(exDate > date){
			 layer.close(i);
			 $("#examineDate").focus();
			 layer.msg("巡检时间不能大于当前时间");
			 return null;
		}
		var fraudDispose;
		if(isfraud==0){
			fraudDispose = "";
		}else{
			fraudDispose = $("#fraudDispose").val();
		} 
		var fkexaminePerson = $("#fkexaminePerson").val();
		var id = $("#entityId_id").val();
		//房间号、客户id预留
		var fkcustomerId = window.parent.parent.$("#customerId").val();
		var fkroomId = window.parent.parent.$("#roomId").val();
		var url = "customer/dm/add";
		$.ajax({
			type : "post",
			async : false,
			url : url,
			dataType : "json",
			data : {
				id : id,
				fkexaminePerson : fkexaminePerson,
				examineDate : examineDate,
				examineContent : examineContent,
				examineRecord : examineRecord,
				isfraud : isfraud,
				fraudDispose : fraudDispose,
				fkcustomerId : fkcustomerId,
				fkroomId : fkroomId,
				opt : optMan,
				remark : remark,
				fraudImg:fraudImg,
				AccessoryCaList:AccessoryCaList,
				rFileList : rFileList
			},
			success : function(responseText) {
				layer.close(layer.index);
				if (responseText.success) {
					layer.alert("保存成功！", {
						offset:'20%',
						shade:0
					}, function() {
						parent.parent.tabToken = 1;
						parent.location.reload();
					});

				} else {
					layer.alert('保存失败！');
				}
			}
		});
	}
</script>
</head>
<body>
	<div id="man_div">
		<div class="panel panel-default">
			<div class="panel-body">
				<input type="hidden" id="decoration_articleId">
				<table cellpadding="10" cellspacing="5" class="mytable">
					<tr>
						<td width="70">巡视时间</td>
						<td><input name="examineDate" id="examineDate" class="Wdate"  readonly="readonly"
							onClick="WdatePicker({ dateFmt: 'yyyy-MM-dd HH:mm:ss'})" /> <span></span>
						</td>
					</tr>
					<tr>
						<!-- 当前登录人    未完待续。。。 -->
						<td width="70">巡视人员</td>
						<td><input name="fkexaminePerson" id="fkexaminePerson"
							value="" /> <span></span></td>
					</tr>
					<tr>
						<td width="70">巡视内容</td>
						<td><select class="js-example-tags" name="examineContent"
							id="examineContent" style="width:135px;" onchange="change(this)">
						</select></td>
					</tr>
					<tr>
						<td width="70" id="examine">人员巡查</td>
						<td><textarea rows="3" cols="70" style="width:70%"
								name="examineRecord" id="examineRecord"></textarea> <span></span>

						</td>
					</tr>
					<tr>
						<td width="70">是否违规</td>
						<td><input type="radio" name="isfraud" id="isfraud" value="0"
							style="cursor:pointer;" checked="checked" onchange="disWeigui()">否 <input
							type="radio" name="isfraud" id="isfraud" value="1"
							style="cursor:pointer;" onchange="disWeigui()">是</td>
					</tr>
					<tr class="weigui" hidden="hidden">
						<td width="70">违规处理</td>
						<td><textarea rows="3" cols="70" style="width:70%"
								name="fraudDispose" id="fraudDispose"></textarea> <span></span>

						</td>
					</tr>
					<tr class="weigui" hidden="hidden">
						<td>违规现象</td>
						<td colspan="3"><input name="fraudImg" id="fraudImg"/>
							 <!-- 上传按钮 -->
							<div id='filePicker_wgxx' style="display:none;">上传</div>
						</td>
					</tr>
					<tr class="weigui" hidden="hidden">
						<td colspan="4">
							<!-- 附件上传 巡检———违规现象。。。--> <!-- 缩略图容器 -->
							<div id='list_wgxx'></div>
						</td>
					</tr>
				</table>
				<!-- 修改时添加变更说明 -->
				<div style="margin:10px;display:none;" id="dm_div" >
					变更说明<textarea rows="3" name="remark" id="dmRemark" style="width:70%"></textarea><span style="color:red;">*</span>
				</div>
				<input type="hidden" id="entityId_id" name="id" value="${id }" />
			</div>
		</div>
		<div style="width:100%;text-align:center; padding-top:20px">
			<div id="btn_div1" style="display:none;">
				<button class="btn btn-danger radius my-btn" onclick="saveMan(0)"
					type="button">提交</button>
				<button class="btn btn-danger radius my-btn" onclick="saveMan(1)"
					type="button">保 存</button>
				<a class="btn btn-default radius" onClick="resetForms('man_div');">
					重置 </a> <br />
			</div>
			<div id="btn_div2" style="display:none;">
				<!-- <button id="modifyBtn" type="button"
					class="btn btn-danger radius my-btn" onclick="saveMan(3)">启用编辑</button> -->
				<button class="btn btn-danger radius my-btn "
					onclick="saveMan(2)" type="button">保存修改</button>
				<button class="btn btn-default radius "
					onClick="resetForms('man_div');">重置</button>
			</div>
		</div>
	</div>

</body>
</html>
